@echo off
%~3

set temp_path=%~1\temp

mkdir "%temp_path%

echo transform latex sources to unicode...
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.tex
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.sty
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.cls
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.bib

echo transform images with imagemagic...
for %%I in ("%~1\images\*.jpg") do convert "%%~I" "%temp_path%\images\%%~nI.png"

cd "%temp_path%"

echo RAW=====================================
echo RAW=============== /b/c2/cXETEX/b/c0/c ===============
echo RAW=====================================
xelatex "%~2" --include-directory "." --output-directory "."
bibtex8 -B -c cp1251.csf "%~2.aux"
ansi2utf8 -in ".\%~2.bbl" -overwrite
ansi2utf8 -in ".\%~2.aux" -overwrite
xelatex "%~2" --include-directory "." --output-directory "."
xelatex "%~2" --include-directory "." --output-directory "."

echo copying %~2.pdf to root directory...
copy /y .\%~2.pdf /b ..\%~2.pdf /b
cd %~1
copy /y .\%~2.pdf /b ..\%~2.pdf /b
rmdir /q /s "%temp_path%"
